﻿using Microsoft.LightSwitch;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;

namespace LightSwitchApplication
{
    public partial class Dashboard
    {
        partial void TotalNoOfBooks_Compute(ref int result)
        {
            // Set result to the desired field value
            if (this.Books != null && this.Books.Any())
            {
                result = this.Books.Count();
            }

        }

        partial void NoOfBooksCompleted_Compute(ref int result)
        {
            // Set result to the desired field value
            if (this.Books != null && this.Books.Any())
            {
                List<Book> completedBooks = (from rows in this.Books where (rows.PercentageOfCompletion * 100) > 99 select rows).ToList<Book>();
                if (completedBooks.Any())
                {
                    result = completedBooks.Count;
                }
                else
                {
                    result = 0;
                }
            }
        }

        partial void NoOfBooksNotStarted_Compute(ref int result)
        {
            // Set result to the desired field value
            if (this.Books != null && this.Books.Any())
            {
                List<Book> notStartedBooks = (from rows in this.Books where (rows.PercentageOfCompletion*100) <= 0 select rows).ToList<Book>();
                if (notStartedBooks.Any())
                {
                    result = notStartedBooks.Count;
                }
                else
                {
                    result = 0;
                }
            }
        }

        partial void NoOfBooksInProgress_Compute(ref int result)
        {
            // Set result to the desired field value
            if (this.Books != null && this.Books.Any())
            {
                List<Book> inprogressBooks = (from rows in this.Books where (rows.PercentageOfCompletion * 100) > 0 && (rows.PercentageOfCompletion * 100) < 99 select rows).ToList<Book>();
                if (inprogressBooks.Any())
                {
                    result = inprogressBooks.Count;
                }
                else
                {
                    result = 0;
                }
            }
        }

        partial void NoOfPagesRead_Compute(ref int result)
        {
            // Set result to the desired field value
            if (this.Books != null && this.Books.Any())
            {
                foreach (Book b in this.Books)
                {
                    result += b.ReadTillPageNumber;
                }
            }
        }

        partial void TotalNoOfPages_Compute(ref int result)
        {
            if (this.Books != null && this.Books.Any())
            {
                foreach (Book b in this.Books)
                {
                    result += b.NumberOfPages;
                }
            }
        }

        partial void NoOfPagesNotRead_Compute(ref int result)
        {
            if (this.Books != null && this.Books.Any())
            {
                foreach (Book b in this.Books)
                {
                    result += b.NumberOfPages - b.ReadTillPageNumber;
                }
            }
        }

        partial void PercentageOfCompletion_Compute(ref decimal result)
        {
            // Set result to the desired field value
            if (this.TotalNoOfPages > 0 && this.NoOfPagesRead > 0)
            {
                decimal noOfP = this.NoOfPagesRead;
                decimal totNoOfP = this.TotalNoOfPages;
                result = noOfP / totNoOfP;
            }
            else
            {
                result = 0;
            }
        }
    }
}